package java.util.stream.lock;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock 精确唤醒线程·
 * <p>
 * 需求:
 * 三个线程按顺序执行任务 A->B->C
 * 并且重复3次
 *
 * @author fangjiaxiaobai@gmail.com
 * @date 2020-05-15
 * @since 1.0.0
 */
public class ReentrantLockDemo {

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(3, 10, 20L,
                        TimeUnit.MINUTES, new ArrayBlockingQueue<>(10), new ThreadFactory() {
                    private volatile AtomicInteger theadNameIndex = new AtomicInteger(0);

                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r,"thread-" + (theadNameIndex.getAndIncrement()));
                    }
                });

        threadPoolExecutor.prestartAllCoreThreads();

        ShareResource shareResource = new ShareResource();

        threadPoolExecutor.execute(() -> {
            for (int i = 0; i < 10; i++) {
                shareResource.taskA();
            }
        });
        threadPoolExecutor.execute(() -> {
            for (int i = 0; i < 10; i++) {
                shareResource.taskB();
            }
        });
        threadPoolExecutor.execute(() -> {
            for (int i = 0; i < 10; i++) {
                shareResource.taskC();
            }
        });

        threadPoolExecutor.shutdown();

    }

}

/**
 * 任务集合
 */
class ShareResource {

    private volatile int number = 1;

    /**
     * 非公平锁
     */
    private ReentrantLock lock = new ReentrantLock();

    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    public void taskA() {

        lock.lock();
        try {
            while (1 != number) {
                c1.await();
            }
            System.out.println(Thread.currentThread().getName() + ":" + number);
            number = 2;
            c2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

    public void taskB() {

        lock.lock();
        try {
            while (2 != number) {
                c2.await();
            }
            number = 3;
            System.out.println(Thread.currentThread().getName() + ":" + number);
            c3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

    public void taskC() {

        lock.lock();
        try {
            while (3 != number) {
                c3.await();
            }
            System.out.println(Thread.currentThread().getName() + ":" + number);
            System.out.println();
            number = 1;
            c1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

}


